package cms.com.yh.contorller;

import cms.common.Constant;
import cms.common.Result;
import cms.mager.domain.User;
import cms.mager.mapper.UserMapper;
import cms.mager.service.UserService;
import cms.mager.service.impl.UserServiceImpl;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

/**
 * @ClassName: UserController.java
 * @author: yuhui
 * @version: 1.0.0
 * @Description: TODO
 * @createTime: 2021年04月20日 01:13:00
 */
@Controller
@RequestMapping("su")
public class UserController {
    @Autowired
    private UserService userService;
    /**
     * 处理分页数据请求
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @RequestMapping("page.do")
    @ResponseBody
    public Result page(Integer page,Integer limit,String realname,Integer role,Integer deleted) throws ServletException, IOException {

        /*---------------【1】一个@ResponseBody解决下面的代码------------------*/
        /*PrintWriter writer = resp.getWriter();
        //将数据转化为JSON消息
        String msg = JSONObject.toJSONString(rs);
        writer.print(msg);
        writer.flush();
        writer.close();*/

        User user = new User();
        user.setRealname(realname);
        user.setRole(role);
        user.setDeleted(deleted);
        Result rs = userService.findUserPage(page,limit,user);
        return rs;
    }

    /**
     * 去user/list.jsp页面
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @RequestMapping("userList.do")
    public String userList() throws ServletException, IOException {
        return "/WEB-INF/view/user/list.jsp";
    }

    /**
     * 功能描述: 添加用户
     * @Author: yuhui  2021/4/9 17:09
     */
    @RequestMapping("addUser.do")
    @ResponseBody
    public Result addUser(String username,String realname,String password,Integer role) throws IOException {
        User user = new User();
        user.setUsername(username);
        user.setRealname(realname);
        user.setPassword(password);
        user.setRole(role);
        UserService uService = new UserServiceImpl();
        Result rt = uService.adduser(user);
        return rt;

        /*PrintWriter writer = resp.getWriter();
        writer.print(JSONObject.toJSONString(rt));
        writer.flush();*/
    }

    /**
     * 功能描述: 还原用户密码
     * @Author: yuhui  2021/4/9 19:11
     */
    @RequestMapping("resetPwd.do")
    @ResponseBody
    public Result resetPwd(Integer id) throws IOException {
        Result result = userService.reSetPwd(id, Constant.USER_DEFAULT_PASSWORD,new Date());
        return result;
    }

    @RequestMapping("motifyPwd.do")
    @ResponseBody
    public Result motifyPwd(String newpwd,String id) throws IOException {
        Result result = userService.updPwd(id, newpwd,new Date());
        return result;
    }

    /**
     * 功能描述: 删除用户
     * @Author: yuhui  2021/4/10 16:50
     */
    @RequestMapping("del.do")
    @ResponseBody
    public Result del(Integer id) throws IOException {
        Result result = userService.delInfoById(id,new Date());
        return result;
    }

    /**
     * 功能描述: 获取所有业务员姓名及ID
     * @Author: yuhui  2021/4/10 16:52
     */
    @RequestMapping("allName.do")
    @ResponseBody
    public Result allName(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Result allName = userService.getAllName();
        return allName;
    }


    /**
     * @title:
     * @description: 接收上传的图片+保存本地+更新数据库图片
     * @author: YH
     * @modifyTime: 2021/4/20 15:56
     */
    @RequestMapping("modifyImg.do")
    @ResponseBody
    public Result modifyImg(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        User user = (User)req.getSession().getAttribute("user");
        Part part = req.getPart("fileImg");
        String fileName = getFileName(part); //拿到文件名

        /*开始组装文件名，并持久化到硬盘*/
        String yyyyMMddHHssmmSSS = DateUtil.format(new Date(), "yyyyMMddHHssmmSSS");//使用Hutool的时间工具类
        String exst = FileUtil.extName(fileName); //获取大文件后缀
        String img = req.getServletContext().getRealPath("/resources/img"); //获取项目的img文件夹真是路径
        String imgFile = yyyyMMddHHssmmSSS + "." + exst;
        String filePath = img + File.separator + imgFile;
        part.write(filePath); //将图片持久化到磁盘

        /*开始操作数据库*/
        Result result = userService.updImg("/resources/img/"+imgFile, user.getId());
        return result;
    }

    /*获取上传的图片名称[ 带后缀 ]*/
    private String getFileName(Part part){
        String header = part.getHeader("Content-Disposition");
        String[] split = header.split(";");
        String[] FileNames = split[2].split("=");
        String fileName = FileNames[FileNames.length - 1];
        return fileName.substring(0,fileName.length()-1);
    }
}
